;
(function() {
	var https = require("https");
	var core = require("./core");
	var xml2js = require('xml2js');
	var fs = require('fs');
	var path = require('path');
	var certFile = fs.readFileSync(path.resolve(__dirname, '.pem/apiclient_cert.pem'));
	var keyFile = fs.readFileSync(path.resolve(__dirname, '.pem/apiclient_key.pem'));
	var request = require("request");
	var parseString = require('xml2js').parseString;
	var Node_PHP = require("../php-to-js/php-for-js");

	//JS支付
	function js_unified_order(info, cb) {
		var self = this;
		var wx_php = new Node_PHP({
			// debug:true
		});
		console.log("js_unified_order:", info);
		wx_php.include(__dirname + "/WxPayPubHelper/WxPayPubHelper.php")
			.add("$unifiedOrder = new UnifiedOrder_pub()")
			.add('$unifiedOrder->setParameter("openid","' + info.openid + '");')
			.add('$unifiedOrder->setParameter("body","' + info.title + '");')
			.add('$unifiedOrder->setParameter("out_trade_no","' + info._id + '");')
			.add('$unifiedOrder->setParameter("total_fee","' + parseInt(info.cash * 100) + '");')
			.add('$unifiedOrder->setParameter("spbill_create_ip","' + info.user_ip + '");')
			.add('$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);')
			.add('$unifiedOrder->setParameter("trade_type","JSAPI");')
			// .get_fun("$unifiedOrder->createXml", [], "hehe")
			.add("echo $unifiedOrder->getPrepayId()")
			.end(function(output) {
				console.log("----PrepayId----");
				console.log(output);
				console.log("----PrepayId----");

				var wx_php = new Node_PHP({
					// debug:true
				});
				wx_php.include(__dirname + "/WxPayPubHelper/WxPayPubHelper.php")
					.add("$jsApi = new JsApi_pub()")
					.add("$jsApi->setPrepayId('" + output + "');")
					.add("$jsApiParameters = $jsApi->getParameters()")
					.add("echo $jsApiParameters")
					.end(function(wx_pay_json) {
						console.log("----chooseWXPay----");
						console.log(wx_pay_json);
						console.log("----chooseWXPay----");
						cb && cb(JSON.parse(wx_pay_json));
					});
			});

	};
	//扫描支付
	function native_unified_order(info, cb) {

		var self = this;
		var wx_php = new Node_PHP({
			// debug:true
		});
		wx_php.include(__dirname + "/WxPayPubHelper/WxPayPubHelper.php")
			.add("$unifiedOrder = new UnifiedOrder_pub()")
			.add('$unifiedOrder->setParameter("body","' + info.title + '");')
			.add('$unifiedOrder->setParameter("out_trade_no","' + info._id + '");')
			.add('$unifiedOrder->setParameter("total_fee","' + parseInt(info.cash * 100) + '");')
			.add('$unifiedOrder->setParameter("spbill_create_ip","' + info.user_ip + '");')
			.add('$unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);')
			.add('$unifiedOrder->setParameter("trade_type","NATIVE");')
			.add("$unifiedOrderResult = $unifiedOrder->getResult();")
			.add("echo json_encode($unifiedOrderResult)")
			.end(function(output) {
				console.log("----unifiedOrderResult----");
				console.log(output);
				console.log("----unifiedOrderResult----");
				cb && cb(JSON.parse(output));
			});
	};
	//支付完成通知接口
	function pay_notify(xmlData, cb) {
		var self = this;
		var wx_php = new Node_PHP({
			// debug:true
		});
		wx_php.include(__dirname + "/WxPayPubHelper/WxPayPubHelper.php")
			.add("$notify = new Notify_pub()")
			.fun("$notify->saveData", [xmlData])
			.add('if($notify->checkSign() == FALSE){')
			.add('$notify->setReturnParameter("return_code","FAIL");')
			.add('$notify->setReturnParameter("return_msg","签名失败");')
			.add('}else{')
			.add('$notify->setReturnParameter("return_code","SUCCESS");')
			.add('}')
			.add("$returnXml = $notify->returnXml();")
			.add("echo $returnXml;")
			.end(function(returnXml) {
				console.log("----returnXml----");
				console.log(returnXml);
				console.log("----returnXml----");
				parseString(xmlData, function(error, requestJson) {
					if (error) {
						throw error;
					}
					parseString(returnXml, function(error, returnJson) {
						if (error) {
							throw error;
						}
						cb && cb(requestJson.xml, returnJson.xml, returnXml);
					});
				});
			});
	};
	module.exports = {
		js_unified_order: js_unified_order,
		native_unified_order: native_unified_order,
		pay_notify: pay_notify
	}

	// 	var xmlData = "<xml><appid><![CDATA[wxf5b5e87a6a0fde94]]></appid>\
	// <bank_type><![CDATA[CFT]]></bank_type>\
	// <fee_type><![CDATA[CNY]]></fee_type>\
	// <is_subscribe><![CDATA[N]]></is_subscribe>\
	// <mch_id><![CDATA[10000097]]></mch_id>\
	// <nonce_str><![CDATA[dz8nirk7gmxhhxn38zgib28yx14ul2gf]]></nonce_str>\
	// <openid><![CDATA[ozoKAt-MmA74zs7MBafCix6Dg8o0]]></openid>\
	// <out_trade_no><![CDATA[wxf5b5e87a6a0fde941409708791]]></out_trade_no>\
	// <result_code><![CDATA[SUCCESS]]></result_code>\
	// <return_code><![CDATA[SUCCESS]]></return_code>\
	// <sign><![CDATA[EDACA525F6C675337B2DAC25B7145028]]></sign>\
	// <sub_mch_id><![CDATA[10000097]]></sub_mch_id>\
	// <time_end><![CDATA[20140903094659]]></time_end>\
	// <total_fee>1</total_fee>\
	// <trade_type><![CDATA[NATIVE]]></trade_type>\
	// <transaction_id><![CDATA[1004400737201409030005091526]]></transaction_id>\
	// </xml>"
	// 	parseString(xmlData + xmlData.replace(/xml/g, "xml2"), function(err, data) {
	// 		console.log(data);
	// 	})
}());